package com.gingko.duominuo.ui.fragment

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.gingko.duominuo.R
import com.gingko.duominuo.databinding.FragmentTaskAutorDialogBinding
import com.gingko.duominuo.utils.dp2px
import com.gingko.duominuo.utils.screenWidth
import com.gingko.duominuo.utils.singleClick


class AuthorDialogFragment : DialogFragment() {
    private var _binding: FragmentTaskAutorDialogBinding? = null
    private var userId: String? = null
    private var userName: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        userId = arguments?.getString(ARG_USER_ID)
        userName = arguments?.getString(ARG_USER_NAME)
    }

    override fun onResume() {
        super.onResume()
        dialog?.window?.setGravity(Gravity.TOP or Gravity.CENTER_HORIZONTAL)
        val attributes = dialog?.window?.attributes?.also {
            it.width = (screenWidth * 0.9).toInt()
            it.height = ViewGroup.LayoutParams.WRAP_CONTENT
            it.y = dp2px(20)
        }
        dialog?.window?.attributes = attributes
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentTaskAutorDialogBinding.inflate(inflater, container, false)
        dialog?.window?.setBackgroundDrawable(ColorDrawable(0x00000000));
        return _binding?.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        userId?.let {
            _binding?.tvAuthorId?.text = it
        }
        userName?.let {
            _binding?.tvAuthorName?.text = it
        }
        _binding?.ivClose?.singleClick {
            dismiss()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        private const val ARG_USER_ID = "userID"
        private const val ARG_USER_NAME = "userName"
        fun newInstance(userId: String?, userName: String?): AuthorDialogFragment {
            val args = Bundle().apply {
                putString(ARG_USER_ID, userId)
                putString(ARG_USER_NAME, userName)
            }
            val fragment = AuthorDialogFragment()
            fragment.arguments = args
            return fragment
        }
    }
}